﻿using System;
using System.Data;
using System.Windows.Forms;
using Util.Database;

namespace ConfigureNZBLoader.EditControls
{
    public partial class Show : BaseEditControl
    {
        public Show()
        {
            InitializeComponent();
        }

        private long _ID;
        public long ID
        {
            get { return _ID; }
        }

        private string _ShowName;
        public string ShowName
        {
            get { return _ShowName; }
        }

        private string _DetailsQuery;
        public string DetailsQuery
        {
            get { return _DetailsQuery; }
        }

        private string _NameQuery;
        public string NameQuery
        {
            get { return _NameQuery; }
        }


        public void Open(long ID)
        {
            //Open existing entry
            _ID = ID;
            using (IDbCommand cmd = DB.CreateCommand())
            {
                cmd.CommandText = "select * from show where id=?";
                IDataParameter par = cmd.CreateParameter();
                par.Value = ID;
                cmd.Parameters.Add(par);

                using (IDataReader dr = cmd.ExecuteReader())
                {
                    if (dr.Read())
                    {
                        _ShowName = dr["NAME"].ToString();
                        _DetailsQuery = dr["DETAILSQUERY"].ToString();
                        _NameQuery = dr["NAMEQUERY"].ToString();
                    }
                    else
                    {
                        throw new ArgumentException("Invalid ID", "ID");
                    }
                }
            }

            txtShowName.Text = _ShowName;
            txtDetailsQuery.Text = _DetailsQuery;
            txtNameQuery.Text = _NameQuery;
        }


        private void btnUpdate_Click(object sender, EventArgs e)
        {
            _ShowName = txtShowName.Text;
            _DetailsQuery = txtDetailsQuery.Text;
            _NameQuery = txtNameQuery.Text;

            using (IDbCommand cmd = DB.CreateCommand())
            {
                cmd.CommandText = "update show set name=?, namequery=?, detailsquery=? where id=?";
                IDataParameter par = cmd.CreateParameter();
                par.Value = ShowName;
                cmd.Parameters.Add(par);
                par = cmd.CreateParameter();
                par.Value = NameQuery;
                cmd.Parameters.Add(par);
                par = cmd.CreateParameter();
                par.Value = DetailsQuery;
                cmd.Parameters.Add(par);
                par = cmd.CreateParameter();
                par.Value = ID;
                cmd.Parameters.Add(par);
                cmd.ExecuteNonQuery();
            }

            ItemChangedNotification(ShowName);

        }

        private void btnNameQuery_Click(object sender, EventArgs e)
        {
            TestQueryString dlg = new TestQueryString();
            dlg.Query = txtNameQuery.Text;
            if (DialogResult.OK == dlg.ShowDialog(this.Parent))
            {
                txtNameQuery.Text = dlg.Query;
            }
        }

        private void btnDetailsQuery_Click(object sender, EventArgs e)
        {
            TestQueryString dlg = new TestQueryString();
            dlg.Query = txtDetailsQuery.Text;
            if (DialogResult.OK == dlg.ShowDialog(this.Parent))
            {
                txtDetailsQuery.Text = dlg.Query;
            }
        }

    }
}
